﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shop.Intermediate;
using Shop.Intermediate.Data;
using Shop.Intermediate.Repository;

namespace Shop.Data.LinqToEntityProvider
{
    [Serializable]
    public class DataProvider:IDataProvider
    {
        private string m_connectionString;
        private ShopObjectContext m_dataContext;
        private IProcedure m_procedure;
        private IDepartmentRepository m_departmentRepository;
        private IBasicTypeRepository m_basicTypeRepository;
        private IBasicDataRepository m_basicDataRepository;
        private IDbHelper m_dbHelper;

        public DataProvider(string connectionString)
        {
            this.m_connectionString = connectionString;
        }
       
        public ShopObjectContext DataContext
        {
            get
            {
                if (m_dataContext == null)
                {
                    m_dataContext = new ShopObjectContext(m_connectionString);
                }
                return m_dataContext;
            }
        }
        public ITransaction Transaction
        {
            get
            {
                //if (m_transaction == null)
                //{
                //    m_transaction = new DbTransaction(m_dataContext);
                //}
                //return m_transaction;

                return new DbTransaction(m_dataContext);
            }
        }
        public IProcedure Procedure
        {
            get
            {
                if (m_procedure == null)
                {
                    m_procedure = new DbProcedure(m_dataContext);
                }
                return m_procedure;
            }
        }
        public IDbHelper DbHelper
        {
            get
            {
                if (m_dbHelper == null)
                {
                    m_dbHelper = new DbHelper();
                }
                return m_dbHelper;
            }
 

        }

        public IDepartmentRepository DepartmentRepository
        {
            get
            {
                if (m_departmentRepository == null)
                {
                    m_departmentRepository = new DepartmentRepository(DataContext);
                }
                return m_departmentRepository;
            }
        }
        public IBasicTypeRepository BasicTypeRepository
        {
            get
            {
                if (m_basicTypeRepository == null)
                {
                    m_basicTypeRepository = new BasicTypeRepository(DataContext);
                }
                return m_basicTypeRepository;
            }
        }

        public IBasicDataRepository BasicDataRepository
        {
            get
            {
                if (m_basicDataRepository == null)
                {
                    m_basicDataRepository = new BasicDataRepository(DataContext);
                }
                return m_basicDataRepository;
            }
        }

        public IDepartment NewDepartment()
        {
            return new Department();
        }

        public IBasicType NewBasicType()
        {
            return new BasicType();
        }

        public IBasicData NewBasicData()
        {
            return new BasicData();
        }

        public void SubmitChanges()
        {
            this.DataContext.SaveChanges();
        }
    }
}
